# -*- coding: UTF-8 -*-
from __future__ import unicode_literals
from django import forms

from  django.contrib.auth.models import User

from article.models import Sort, Article


# 注册表单
class UserRegisterForm(forms.Form):
    username = forms.CharField(required=True, label='用户名', error_messages={'required': '请输入用户名'}, widget=
    forms.TextInput(attrs={'placeholder': '用户名'})
                               )
    password = forms.CharField(required=True, label='密码', error_messages={'required': u'请输入密码'}, widget=
    forms.PasswordInput(attrs={'placeholder': '密码'})
                               )
    repeat_password = forms.CharField(required=True, label='重复密码', error_messages={'required': '请再次输入密码'}, widget=
    forms.PasswordInput(attrs={'placeholder': '密码'}))

    # clean_username(self)在form.username执行完了执行.
    def clean_username(self):
        username = self.cleaned_data['username']
        is_exist = User.objects.filter(username=username).exists()
        if is_exist:
            raise forms.ValidationError('用户名已存在，请重新输入。')
        else:
            return username

    # 同理,clean_password(self)在form.password执行完了执行,因此若命名clean_password将得不到repeat_password值.
    def clean_repeat_password(self):
        password = self.cleaned_data['password']
        repeat_password = self.cleaned_data['repeat_password']
        if not password == repeat_password:
            raise forms.ValidationError('两次输入的密码不相同，请重新输入。')
        else:
            return password, repeat_password


class UserCreateForm(forms.ModelForm):
    class Meta:
        model = User
        fields = ('username', 'password')
        widgets = {'password': forms.PasswordInput(), }
        error_messages = {'username': {'required': '请输入用户名'}, 'password': {'required': '请输入密码'}}

    repeat_password = forms.CharField(label='重复密码',error_messages={'required': '请再次输入密码'},
                                      widget=forms.PasswordInput())

    # clean_username(self)在form.username执行完了执行.
    def clean_username(self):
        username = self.cleaned_data['username']
        is_exist = User.objects.filter(username=username).exists()
        if is_exist:
            raise forms.ValidationError('用户名已存在，请重新输入。')
        else:
            return username

    # 同理,clean_password(self)在form.password执行完了执行,因此若命名clean_password将得不到repeat_password值.
    def clean_repeat_password(self):
        password = self.cleaned_data['password']
        repeat_password = self.cleaned_data['repeat_password']
        if not password == repeat_password:
            raise forms.ValidationError('两次输入的密码不相同，请重新输入。')
        else:
            return password, repeat_password


# 文章add表单
# class ArticleAddForm(forms.Form):
#     title = forms.CharField(max_length=30,label='标题',widget=forms.TextInput(attrs={'placeholder':'标题'}),required=True,
#                             error_messages={'required':'请输入标题'})
#     cover_img = forms.ImageField(label='封面图',required=True,error_messages={'required':'请添加封面图'})
#     content = forms.CharField(label='内容',required=True,widget=forms.Textarea(attrs={'rows':9,'cols':29,'placeholder':'内容'}),
#                               error_messages={'required': '请输入内容'})
#     sort = forms.ModelChoiceField(label='分类',queryset=Sort.objects.all())
#
#     def clean_title(self):
#         title = self.cleaned_data['title']
#         if len(title)>30:
#             raise forms.ValidationError('标题长度大于30字符,请重新输入')
#         else:
#             return title


# 第二种方法:
class ArticleAddForm(forms.ModelForm):
    class Meta:
        model = Article
        fields = ('title', 'cover_img', 'content', 'sort')
        error_messages = {'title': {'required': '请输入标题'}, 'cover_img': {'required': '请添加封面图'},
                          'content': {'required': '请输入内容'}}

    def clean_title(self):
        title = self.cleaned_data['title']
        if len(title) > 30:
            raise forms.ValidationError('标题长度大于30字符,请重新输入')
        else:
            return title
